import es
import os
import time
# =========
# GG imports
# =========
from ggConst import *
import ggExceptions
'''
A logging library for GunGame Branch
'''

class GGlogError(ggExceptions._ggException):
    '''
    Flings out an error .. 
     - not making use of self._log for obvious reasons
     - self._display isn't really needed either
    '''
    pass
    
class _ggLogging(object):
    '''
    Basic logging class
    '''
    DEBUG       = 4
    ERROR       = 3
    WARNING     = 2
    INFO        = 1
    file        = None
    filepath    = LOGPATH
    datefrmt    = '%a, %d %b %Y %H:%M:%S'
    level       = DEBUG
    
    def __init__(self, level=None, file=None, datefrmt=None):
        '''
        Sets up the class - most important is the level
        '''
        if datefrmt:
            self.datefrmt = datefrmt
        if level:
            self.level = level
        if file:
            self.file = file
        else:
            # logging into a date formatted file
            self.file = time.strftime('%d_%b_%Y') + ".log"
        # check the damn default logs direcory exists...
        if not os.path.exists(filepath):
            os.mkdir(filepath)
    def _log(self, message):
        '''
        Logs a message to the file with a timestamp
        - Not wonderful atm as there is no error catching if the file pointer errors (or if the dir doesnt exist)
             ^^ some error catching for log directory in __init__... can be missed out tho
        '''
        file_loc = os.path.join(self.filepath, self.file)
        if not os.path.exists(file_loc):
            # create the log fiel if it doesnt exist
            fp = open(file_loc,"wb")
            fp.write("# ===============================================\r")
            fp.write("Created GunGame Log File [" + time.strftime(self.datefrmt) + "]\r")
            fp.write("# ===============================================\r")
            fp.write("# ===============================================\r")
        else:
            fp = open(file_loc,"a")
        fp.write("%s %s \r" % (time.strftime(self.datefrmt), message))
        fp.close()
 
    def debug(self, message):
        '''
        Logs a debug level message
         - Just a handy interface to  self._log()
        '''
        if self.level >= self.DEBUG:
            self._log("DEBUG   " + message)
            
    def error(self, message):
        '''
        Logs an error level message
         - Just a handy interface to  self._log()
        '''
        if self.level >= self.ERROR:
            self._log("ERROR   " + message)
            
    def warning(self, message):
        '''
        Logs a warning level message
         - Just a handy interface to self._log()
        '''
        if self.level >= self.WARNING:
            self._log("WARNING" + message)
            
    def info(self, message):
        '''
        Logs an info level message
         - Just a handy interface to self._log()
        '''
        if self.level >= self.INFO:
            self._log("INFO    " + message)
            
    def setLoggingLevel(self, newlevel):
        self.level = newlevel
        
    def setLogPath(self, newpath)
        if os.path.exists(newpath):
            self.filepath = newpath
        else:
            raise GGlogError, "Unable to set new log storage path. The path does no exist"

